<template>
  <div>
    <el-table
      :row-class-name="tableRowClassName"
      ref="table"
      :data="tableData"
      :span-method="objectSpanMethod"
      border
      :header-cell-style="{ textAlign: 'center', backgroundColor: '#F5F7FA' }"
    >
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="分配类型" align="center" prop="allocType"
        ><template slot-scope="scope"> 按Sku </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="productType">
        <template slot-scope="scope">
          <div>
            {{ scope.row.productType | state1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品编码" align="center" prop="productCode" />
      <!-- <el-table-column label="上架ID" align="center" prop="shelfId" /> -->
      <el-table-column
        label="上架数量"
        align="center"
        prop="productStockCount"
      />
      <el-table-column label="店铺" align="center" prop="storeName" />
      <el-table-column label="退货数量" align="center" prop="quantityCount" />
      <el-table-column label="退货比例" align="center" prop="ratio" />
      <el-table-column
        label="计算分配数量"
        align="center"
        prop="allAllocationCount"
      />
    </el-table>

    <div style="width: 100%; text-align: center">
      <el-button style="margin-top: 12px" @click="prev">上一步</el-button>
      <el-button style="margin-top: 12px" @click="next">提交分配</el-button>
    </div>
  </div>
</template>
<script>
import {
  getShopReturnNumAndAndCalc,
  getShopReturnNumAndAndCalcList,
} from "@/api/productAllocation/BiProductReturnRestock";
import { abnormalTaskProcessList } from "@/api/productAllocation/task";
export default {
  props: {
    row: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 存放所有的表头 一定要与tableData一致 "shelfId",
      colFields: [
        "id",
        "allocType",
        "productType",
        "productCode",

        "productStockCount",
        "storeName",
        "quantityCount",
        "ratio",
        "allAllocationCount",
      ],
      spanArr: [], //存储合并单元格的开始位置
      // 表格数据
      tableData: [],
    };
  },
  filters: {
    state1(string) {
      if (string == "Good") {
        return "良品";
      }
      if (string == "Defective") {
        return "瑕疵";
      }
    },
  },
  watch: {
    row: {
      handler: function (newnew, oldold) {
        if (this.row.length > 0) {
          this.getList();
        }
      },
      deep: true,
      immediate: true,
    },
    // spanArr: {
    //   handler: function (newnew, oldold) {
    //     // console.log(newnew);
    //     // this.showColumn = newnew;
    //     // 这里需要让表格重新绘制一下，否则会产生固定列错位的情况
    //     // console.log(new Date());
    //     this.$nextTick(() => {
    //       this.$refs.table.doLayout();
    //       // console.log('渲染完成时间', new Date());
    //     });
    //   },
    //   deep: true,
    //   immediate: true,
    // },
  },

  // created() {
  //   this.getList();
  // },
  mounted() {
    this.getSpanArr();
    // this.$nextTick(() => {
    //   this.$refs.table.doLayout();
    //   // console.log('渲染完成时间', new Date());
    // });
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (row.id % 2 === 1) {
        // 奇数
        return "warning-row";
      } else {
        // 偶数
        return "";
      }
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载，请稍候！");
    },
    getList() {
      this.openLoading();
      getShopReturnNumAndAndCalcList(this.row).then((response) => {
        this.$modal.closeLoading();
        this.tableData = response;
        this.getSpanArr();
      });
    },
    prev() {
      //   this.handleStatusChange();
      this.$emit("prev");
    },
    next() {
      this.handleStatusChange();

      this.$emit("next");
    },
    handleStatusChange() {
      // console.log(JSON.stringify(this.tableData));
      abnormalTaskProcessList(this.tableData).then((response) => {
        if (response.code == 200) {
          this.$alert("提交分配成功。", "提示", {
            confirmButtonText: "我知道了",
            showConfirmButton: true,
          });
        }
      });
    },
    /**
     * 分析每一列，找出相同的
     * @param data
     */
    getSpanArr() {
      for (let i = 0; i < this.tableData.length; i++) {
        let row = i;
        // let col = i % this.colCount;
        if (row === 0) {
          // i 表示行 j表示列
          for (let j = 0; j < this.colFields.length; j++) {
            this.spanArr[i * this.colFields.length + j] = {
              rowspan: 1,
              colspan: 1,
            };
          }
        } else {
          for (let j = 0; j < this.colFields.length; j++) {
            // 当前和上一次的一样
            //  合并所有列的相同数据单元格 this.colFields[j] == "shelfId" ||
            if (
              this.colFields[j] == "id" ||
              this.colFields[j] == "allocType" ||
              this.colFields[j] == "productType" ||
              this.colFields[j] == "productCode" ||
              this.colFields[j] == "productStockCount"
            ) {
              // 指定合并哪些列
              if (this.tableData[row]["id"] !== this.tableData[row - 1]["id"]) {
                // 哪些不合并:School不一样的,不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              } else if (
                this.tableData[row][this.colFields[j]] ===
                this.tableData[row - 1][this.colFields[j]]
              ) {
                let beforeItem =
                  this.spanArr[(row - 1) * this.colFields.length + j];
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1 + beforeItem.rowspan, // 合并几列
                  colspan: 1, // 合并几行
                };
                beforeItem.rowspan = 0;
                beforeItem.colspan = 0;
              } else {
                // rowspan 和 colspan 都为1表格此单元格不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              }
            }
          }
        }
      }
      // 对数据进行倒序
      let stack = [];
      for (let i = 0; i < this.colFields.length; i++) {
        for (let j = 0; j < this.tableData.length; j++) {
          // console.log("i=" + i + " j=" + j);
          // i 表示列 j表示行
          if (j === 0) {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            }
          } else {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            } else {
              stack.push(this.spanArr[j * this.colFields.length + i]);
              while (stack.length > 0) {
                let pop = stack.pop();
                let len = stack.length;
                this.spanArr[(j - len) * this.colFields.length + i] = pop;
              }
            }
          }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(this.spanArr[rowIndex * this.colFields.length + columnIndex]);
      return this.spanArr[rowIndex * this.colFields.length + columnIndex];
    },
  },
};
</script>
<style lang="scss" >
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>